package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.AdminAuthService;
import com.cskaoyan.market.service.impl.AdminAuthServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/auth/*")
public class AdminAuthController extends HttpServlet {

    private AdminAuthService adminAuthService = new AdminAuthServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/auth/", "");
        switch (replace) {
            case "info":
                info(req,resp);
                break;
        }
    }

    private void info(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("admin");
        Map<String, Object> data = new HashMap<>();
        data.put("roles", new String[]{"超级管理员"});
        data.put("name", admin.getUsername());
        data.put("perms", new String[]{"*"});
        data.put("avatar", admin.getAvatar());
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/auth/", "");
        switch (replace) {
            case "login":
                login(req,resp);
                break;
            case "logout":
                logout(req,resp);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object ok = ResponseUtil.ok(null);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String username = JacksonUtil.parseString(requestBody,"username");
        String password = JacksonUtil.parseString(requestBody,"password");
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            Object o = ResponseUtil.fail(605, "用户名或者密码不能为空");
            String s = JacksonUtil.writeValueAsString(o);
            resp.getWriter().println(s);
            return;
        }
        MarketAdmin marketAdmin = adminAuthService.login(username,password);
        if(marketAdmin == null)
        {
            Object o = ResponseUtil.fail(605, "用户帐号或密码不正确");
            String s = JacksonUtil.writeValueAsString(o);
            resp.getWriter().println(s);
            return;
        }
        HttpSession session = req.getSession();
        session.setAttribute("admin",marketAdmin);
        String id = session.getId();
        Map<String, Object> data = new HashMap<>();
        Map<String, String> info = new HashMap<>();
        info.put("nickName", marketAdmin.getUsername());
        info.put("avatar", marketAdmin.getAvatar());
        data.put("adminInfo", info);
        data.put("token", id);
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }
}
